<?php

namespace App\Http\Controllers;

use App\Models\Appointment;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;

class AppointmentController extends Controller
{
    function appointmentsubmit(Request $req)
    {
        $rules = [
            'username' => 'required',
            'dressname' => 'required',
            'appointmenttime' => 'required',
            'tel' => 'required',
            'address' => 'required'
        ];
        $appointment = [
            'username' => $req->name,
            'dressname' => $req->dressname,
            'appointmenttime' => $req->rentDate,
            'tel' => $req->telNumber,
            'address' => $req->address,
            'state' => '订单待完成'
        ];
        $rule = Validator::make($appointment, $rules);
        if ($rule->fails()) {
            return response()->json(['code' => 201, 'msg' => '请将预约信息补充完整']);
        } else {
            // 验证通过
            Appointment::create($appointment);
            return response()->json(['code' => 200, 'msg' => '预约成功']);
        }
    }
    // 查看预定记录
    function showRentRecordInfo(Request $req)
    {
        $rentRecordInfo = Appointment::where('username', $req->username)->orderBy('appointmenttime', 'desc')->get();
        if (empty($rentRecordInfo)) {
            return response()->json(['code' => 201, 'msg' => '获取成功,但暂无预约信息']);
        } else {
            return response()->json(['code' => 200, 'msg' => '获取成功', 'data' => $rentRecordInfo]);
        }
    }
    // 修改订单状态
    function updateState(Request $req)
    {
        $appointmentid = Appointment::find($req->appointmentid);
        $appointmentid->state = '订单已完成';
        $appointmentid->save();
        return response()->json(['code' => 200, 'msg' => '修改订单状态成功']);
    }

    // web
    public function update(Request $req)
    {
        $updateAppoinmentInfo =  Appointment::where('appointmentid', $req->appointmentid)->get();
        return response()->json($updateAppoinmentInfo);
    }

    public function save(Request $req)
    {
        $updateAppoinmentInfo =  Appointment::find($req->appointmentid);
        $updateAppoinmentInfo->username = $req->username;
        $updateAppoinmentInfo->dressname = $req->dressname;
        $updateAppoinmentInfo->appointmenttime = $req->appointmenttime;
        $updateAppoinmentInfo->tel = $req->tel;
        $updateAppoinmentInfo->address = $req->address;
        $updateAppoinmentInfo->save();
        return response()->json([['code' => 200, 'msg' => '修改信息成功']]);
    }

    public function remove(Request $req)
    {
        $remove = Appointment::where('appointmentid', $req->appointmentid)->delete();
        if ($remove) {
            return response()->json([['code' => 200, 'msg' => '删除成功']]);
        } else {
            return response()->json([['code' => 201, 'msg' => '删除失败']]);
        }
    }

    public function search(Request $req)
    {
        $appointmentInfo = Appointment::where('username', 'like', '%' . $req->keyword . '%')->get();
        return response()->json($appointmentInfo);
    }
}
